import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
import { Context } from "@midwayjs/koa";
import { RespCodeMap } from "./response.code";
import { v5 } from "uuid";
import { SnowFlake } from "./snowflake";

interface RespOptions {
  data?: any;
  meta?: any;
  code?: string;
  message?: string;
}

const response = (options: RespOptions & { success?: boolean } = {}) => {
  return options.success ? response.success(options) : response.fail(options);
};

response.success = (options: RespOptions = {}) => {
  return {
    success: true,
    message:
      options.message || Utils.RESP_CODE_MAP.COMMON_FETCH_SUCCESS.message,
    data: options.data || null,
    meta: options.meta || {},
    code: options.code || Utils.RESP_CODE_MAP.COMMON_FETCH_SUCCESS.code,
    timestamp: Date.now()
  };
};
response.fail = (options: RespOptions = {}) => {
  return {
    success: false,
    message: options.message || Utils.RESP_CODE_MAP.COMMON_FETCH_FAIL.message,
    data: options.data || null,
    meta: options.meta || {},
    code: options.code || Utils.RESP_CODE_MAP.COMMON_FETCH_FAIL.code,
    timestamp: Date.now()
  };
};
response.unauthorize = (ctx: Context) => {
  ctx.response.status = 401;
  ctx.response.body = response.fail({
    message: Utils.RESP_CODE_MAP.UNAUTHORIZED_ERROR.message,
    code: Utils.RESP_CODE_MAP.UNAUTHORIZED_ERROR.code
  });
};

@Provide()
@Scope(ScopeEnum.Request, {
  allowDowngrade: true
})
export class Utils {
  static RESP_CODE_MAP = RespCodeMap;

  @Inject()
  snowFlake: SnowFlake;

  uuid() {
    return v5(
      (Date.now() * Math.random() * Math.random() * Math.random()).toString(),
      v5.URL
    );
  }

  /**
   * 接口操作返回工具
   */
  // response = {
  //   /**
  //    * 接口请求成功返回结果
  //    * @param options
  //    * @returns
  //    */
  //   success(options: RespOptions = {}) {
  //     return {
  //       success: true,
  //       message:
  //         options.message || Utils.RESP_CODE_MAP.COMMON_FETCH_SUCCESS.message,
  //       data: options.data || null,
  //       meta: options.meta || {},
  //       code: options.code || Utils.RESP_CODE_MAP.COMMON_FETCH_SUCCESS.code,
  //       timestamp: Date.now()
  //     };
  //   },

  //   /**
  //    * 接口请求失败返回结果
  //    * @param options
  //    * @returns
  //    */
  //   fail(options: RespOptions = {}) {
  //     return {
  //       success: false,
  //       message:
  //         options.message || Utils.RESP_CODE_MAP.COMMON_FETCH_FAIL.message,
  //       data: options.data || null,
  //       meta: options.meta || {},
  //       code: options.code || Utils.RESP_CODE_MAP.COMMON_FETCH_FAIL.code,
  //       timestamp: Date.now()
  //     };
  //   },

  //   unauthorize(ctx: Context) {
  //     ctx.response.status = 401;
  //     ctx.response.body = this.fail({
  //       message: Utils.RESP_CODE_MAP.UNAUTHORIZED_ERROR.message,
  //       code: Utils.RESP_CODE_MAP.UNAUTHORIZED_ERROR.code
  //     });
  //   }
  // };

  response = response;

  /**
   * 业务操作返回工具
   */
  service = {
    /**
     * 业务操作成果返回结果
     * @param data
     * @param message
     * @returns
     */
    success<T>(data: T, message: string = "") {
      return { success: true, data, message };
    },

    /**
     * 业务操作失败返回结果
     * @param data
     * @param message
     * @returns
     */
    fail(data: any = null, message: string = "") {
      return { success: false, data, message };
    }
  };
}

export const utils = new Utils();
